/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package italk.ui.vidw.panel;

import italk.client.bean.State.ClientState;
import italk.ui.ClientFrame;
import italk.ui.action.ActionManager;
import italk.ui.action.CancelAction;
import italk.ui.view.ViewFactory;
import italk.util.LanguageLoader;
import italk.util.Threads;


/**
 *
 * @author chenzhihui
 */
public class LoginProgressPanel extends javax.swing.JPanel {

    /**
     * Creates new form LoginProgressPanel
     */
    public LoginProgressPanel() {
        initComponents();
        init();
        initAction();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        containerPanel = new javax.swing.JPanel();
        loginProgressBar = new javax.swing.JProgressBar();
        lblProgressState = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        buttomPanel = new javax.swing.JPanel();

        setPreferredSize(new java.awt.Dimension(255, 555));

        topPanel.setPreferredSize(new java.awt.Dimension(255, 150));

        jLabel2.setFont(jLabel2.getFont().deriveFont(jLabel2.getFont().getSize()+13f));
        jLabel2.setText("Logging");

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, topPanelLayout.createSequentialGroup()
                .addContainerGap(67, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(50, 50, 50))
        );

        lblProgressState.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblProgressState.setText("Please waiting...");

        btnCancel.setText(LanguageLoader.getString("cancel"));
        btnCancel.setActionCommand(LanguageLoader.getString("cancel"));

        javax.swing.GroupLayout containerPanelLayout = new javax.swing.GroupLayout(containerPanel);
        containerPanel.setLayout(containerPanelLayout);
        containerPanelLayout.setHorizontalGroup(
            containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(containerPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                    .addComponent(lblProgressState, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(27, 27, 27))
            .addGroup(containerPanelLayout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(btnCancel)
                .addContainerGap())
        );
        containerPanelLayout.setVerticalGroup(
            containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(containerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addComponent(lblProgressState)
                .addGap(24, 24, 24)
                .addComponent(btnCancel)
                .addContainerGap(161, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout buttomPanelLayout = new javax.swing.GroupLayout(buttomPanel);
        buttomPanel.setLayout(buttomPanelLayout);
        buttomPanelLayout.setHorizontalGroup(
            buttomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        buttomPanelLayout.setVerticalGroup(
            buttomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 101, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(topPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(containerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(buttomPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(containerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttomPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JPanel buttomPanel;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblProgressState;
    private javax.swing.JProgressBar loginProgressBar;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables
    public Thread t = null;
    
    /*
     * (non-Javadoc) 
     */
    public void init() {
        t =  new Thread(new Runnable() {

            @Override
            public void run() {
                ClientState.setCurrentState(ClientState.LOGGING);
                lblProgressState.setText("请稍后...");
                if(isCancel()) return ;
                ClientFrame clientFrame = ViewFactory.getClientFrame();
                MainPanel mainPanel = ViewFactory.getMainPanel();
                lblProgressState.setText("正在初始化好友列表...");
                if(isCancel()) return ;
                mainPanel.initContacts();
                setAutoProgressBar(30);
                lblProgressState.setText("正在初始化群与讨论组列表...");
                if(isCancel()) return ;
                mainPanel.initGroups();
                setAutoProgressBar(60);
                lblProgressState.setText("正在初始化最近联系人列表...");
                if(isCancel()) return ;
                mainPanel.initRecentContacts();
                setAutoProgressBar(80);
                lblProgressState.setText("正在完成登录...");
                if(isCancel()) return ;
                setAutoProgressBar(100);
                clientFrame.setContentPane(mainPanel);
                clientFrame.validate();
                clientFrame.repaint();
            }
            
            boolean isCancel() {
                if(ClientState.getCurrentState() == ClientState.CANCEL) {
                    return true;
                }
                return false;
            }
        });
        Threads.submit(t);
    }

    /*
     * (non-Javadoc)
     */
    public void initAction() {
        CancelAction cancelAction = (CancelAction) ActionManager.getAction(CancelAction.class);
        cancelAction.setData(this);
        this.btnCancel.setAction(cancelAction);
    }
    
    private void setAutoProgressBar(int value) {
        for(int i=loginProgressBar.getValue(); i<value; i++) {
            loginProgressBar.setValue(value);
        }
    }
}
